/**
 * 修饰静态方法
 */
public class SynchronizedExample3 {
    static int count = 0;

    synchronized public static void increment () {
        count++;
    }

    public static int getCount () {
        return count;
    }

    public static void main(String[] args) {

        SynchronizedExample3 example = new SynchronizedExample3();

        Thread thread1 = new Thread(() -> {
            for (int i = 0; i < 10000; i++) {
                example.increment();
            }
        });
        Thread thread2 = new Thread(() -> {
            for (int i = 0; i < 10000; i++) {
                example.increment();
            }
        });

        thread1.start();
        thread2.start();

        try {
            thread1.join();
            thread2.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        System.out.println(example.getCount());
    }
}
